package cn.iocoder.yudao.module.pay.controller.admin.point;

import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pay.controller.admin.point.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.point.PayPointRechargeDO;
import cn.iocoder.yudao.module.pay.service.point.PayPointRechargeService;

@Tag(name = "管理后台 - 会员钻石充值")
@RestController
@RequestMapping("/pay/point-recharge")
@Validated
public class PayPointRechargeController {

    @Resource
    private PayPointRechargeService pointRechargeService;

    @PostMapping("/create")
    @Operation(summary = "创建会员钻石充值")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:create')")
    public CommonResult<Long> createPointRecharge(@Valid @RequestBody PayPointRechargeSaveReqVO createReqVO) {
        return success(pointRechargeService.createPointRecharge(createReqVO));
    }

    @PostMapping("/update-paid")
    @Operation(summary = "更新钱包充值为已充值") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录， 内部校验实现
    public CommonResult<Boolean> updatePointRechargerPaid(@Valid @RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        pointRechargeService.updatePointRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
                notifyReqDTO.getPayOrderId());
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员钻石充值")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:update')")
    public CommonResult<Boolean> updatePointRecharge(@Valid @RequestBody PayPointRechargeSaveReqVO updateReqVO) {
        pointRechargeService.updatePointRecharge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员钻石充值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:delete')")
    public CommonResult<Boolean> deletePointRecharge(@RequestParam("id") Long id) {
        pointRechargeService.deletePointRecharge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员钻石充值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:query')")
    public CommonResult<PayPointRechargeRespVO> getPointRecharge(@RequestParam("id") Long id) {
        PayPointRechargeDO pointRecharge = pointRechargeService.getPointRecharge(id);
        return success(BeanUtils.toBean(pointRecharge, PayPointRechargeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员钻石充值分页")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:query')")
    public CommonResult<PageResult<PayPointRechargeRespVO>> getPointRechargePage(@Valid PayPointRechargePageReqVO pageReqVO) {
        PageResult<PayPointRechargeDO> pageResult = pointRechargeService.getPointRechargePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayPointRechargeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员钻石充值 Excel")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPointRechargeExcel(@Valid PayPointRechargePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayPointRechargeDO> list = pointRechargeService.getPointRechargePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员钻石充值.xls", "数据", PayPointRechargeRespVO.class,
                        BeanUtils.toBean(list, PayPointRechargeRespVO.class));
    }

}